package com.yfve.eol.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.yfve.eol.EolApplication;
import com.yfve.eol.R;
import com.yfve.eol.util.Util;

// 0（Red）
// 1（Green）
// 2 （Blue）
// 3（White）
// 4（Black）
// 5（Color bars）
// 6（Gray scale）
// 0xFF（ReturnControlToECU）

public class TftDisplayPatternActivity extends Activity {
    private static final String TAG = "Eol:TftDisplayPatternActivity";
    private static final String EXTRA_STRING_CMD_INDEX = "extra_string_cmd_index";
    @SuppressLint("StaticFieldLeak")
    private static TftDisplayPatternActivity sInstance;
    private View[] mColorBlocks = new View[16];
    private ImageView mBackgroundImageView;
    private int mCmdIndex = -1;

    public static void startTftDisplayPatternActivityHelper(int cmdIndex) {
        Log.i(TAG, "startTftDisplayPatternActivityHelper: " + cmdIndex);
        Intent intent = new Intent(EolApplication.getAppContext(), TftDisplayPatternActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(EXTRA_STRING_CMD_INDEX, cmdIndex);
        EolApplication.getAppContext().startActivity(intent);
    }

    public static void handleSetLCDCtrlSts(int id, byte[] values) {
        Log.i(TAG, "handleSetLCDCtrlSts: id = " + id);
        startTftDisplayPatternActivityHelper(values[0]);
        Util.setByteProperty(id, true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tft_dislay_pattern);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);

        mColorBlocks[0] = findViewById(R.id.color_block_0);
        mColorBlocks[1] = findViewById(R.id.color_block_1);
        mColorBlocks[2] = findViewById(R.id.color_block_2);
        mColorBlocks[3] = findViewById(R.id.color_block_3);
        mColorBlocks[4] = findViewById(R.id.color_block_4);
        mColorBlocks[5] = findViewById(R.id.color_block_5);
        mColorBlocks[6] = findViewById(R.id.color_block_6);
        mColorBlocks[7] = findViewById(R.id.color_block_7);
        mColorBlocks[8] = findViewById(R.id.color_block_8);
        mColorBlocks[9] = findViewById(R.id.color_block_9);
        mColorBlocks[10] = findViewById(R.id.color_block_10);
        mColorBlocks[11] = findViewById(R.id.color_block_11);
        mColorBlocks[12] = findViewById(R.id.color_block_12);
        mColorBlocks[13] = findViewById(R.id.color_block_13);
        mColorBlocks[14] = findViewById(R.id.color_block_14);
        mColorBlocks[15] = findViewById(R.id.color_block_15);

        mCmdIndex = getIntent().getIntExtra(EXTRA_STRING_CMD_INDEX, 0);
        Log.i(TAG, "onCreate: mCmdId = " + mCmdIndex);

        drawColorBlockByCmdIndex(mCmdIndex);
        sInstance = this;
    }

    @Override
    protected void onDestroy() {
        sInstance = null;
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mCmdIndex = getIntent().getIntExtra(EXTRA_STRING_CMD_INDEX, 0);
        Log.i(TAG, "onNewIntent: mCmdId = " + mCmdIndex);
//        if (mCmdIndex == 255) {
//            finish();
//            return;
//        }
        drawColorBlockByCmdIndex(mCmdIndex);

    }

    private void drawColorBlockByCmdIndex(int cmdIndex) {
        switch (cmdIndex) {
            case 0:
                drawFullColor(R.color.colorRed);
                break;
            case 1:
                drawFullColor(R.color.colorGreen);
                break;
            case 2:
                drawFullColor(R.color.colorBlue);
                break;
            case 3:
                drawFullColor(R.color.colorWhite);
                break;
            case 4:
                drawFullColor(R.color.colorBlack);
                break;
            case 5:
                drawColorBars();
                break;
            case 6:
                drawGrayScale();
                break;
            default:
                Log.e(TAG, "drawColorBlockByCmdIndex: error index " + cmdIndex);
                finish();
                break;
        }
    }

    private void drawFullColor(int colorRes) {
        for (View colorBlock : mColorBlocks) {
            colorBlock.setBackgroundColor(ContextCompat.getColor(this, colorRes));
        }
    }

    //color bars alternatve pattern color definition
    //white, black, yellow, cyan, green, magenta, red, blue
    private void drawColorBars() {
        mColorBlocks[0].setBackgroundColor(ContextCompat.getColor(this, R.color.colorRed));
        mColorBlocks[1].setBackgroundColor(ContextCompat.getColor(this, R.color.colorRed));
        mColorBlocks[2].setBackgroundColor(ContextCompat.getColor(this, R.color.colorBlack));
        mColorBlocks[3].setBackgroundColor(ContextCompat.getColor(this, R.color.colorBlack));
        mColorBlocks[4].setBackgroundColor(ContextCompat.getColor(this, R.color.colorYellow));
        mColorBlocks[5].setBackgroundColor(ContextCompat.getColor(this, R.color.colorYellow));
        mColorBlocks[6].setBackgroundColor(ContextCompat.getColor(this, R.color.colorCyan));
        mColorBlocks[7].setBackgroundColor(ContextCompat.getColor(this, R.color.colorCyan));
        mColorBlocks[8].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGreen));
        mColorBlocks[9].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGreen));
        mColorBlocks[10].setBackgroundColor(ContextCompat.getColor(this, R.color.colorMagenta));
        mColorBlocks[11].setBackgroundColor(ContextCompat.getColor(this, R.color.colorMagenta));
        mColorBlocks[12].setBackgroundColor(ContextCompat.getColor(this, R.color.colorRed));
        mColorBlocks[13].setBackgroundColor(ContextCompat.getColor(this, R.color.colorRed));
        mColorBlocks[14].setBackgroundColor(ContextCompat.getColor(this, R.color.colorBlue));
        mColorBlocks[15].setBackgroundColor(ContextCompat.getColor(this, R.color.colorBlue));
    }

    private void drawGrayScale() {
        mColorBlocks[0].setBackgroundColor(ContextCompat.getColor(this, R.color.colorBlack));
        mColorBlocks[1].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey1));
        mColorBlocks[2].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey2));
        mColorBlocks[3].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey3));
        mColorBlocks[4].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey4));
        mColorBlocks[5].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey5));
        mColorBlocks[6].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey6));
        mColorBlocks[7].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey7));
        mColorBlocks[8].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey8));
        mColorBlocks[9].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey9));
        mColorBlocks[10].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey10));
        mColorBlocks[11].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey11));
        mColorBlocks[12].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey12));
        mColorBlocks[13].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey13));
        mColorBlocks[14].setBackgroundColor(ContextCompat.getColor(this, R.color.colorGrey14));
        mColorBlocks[15].setBackgroundColor(ContextCompat.getColor(this, R.color.colorWhite));
    }
}
